<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Admin;
use Illuminate\Support\Facades\DB;

class AdminController extends Controller
{
    public function _list(Request $request){
        $cond = [];
        $search_data = [];
        if($request->filled('username')){
            $cond[] = ['username','like','%'.$request->username.'%'];
            $search_data['username'] = $request->username;
        }
        $list = Admin::where($cond)->orderBy('created_at','desc')->get();
        return view('Admin.list',[
            'list' => $list,
            'search_data' => $search_data
        ]);
    }

    public function edit(Request $request){
        $id = $request->filled('id') ? $request->id : null;

        if(!$_POST){
            $info = Admin::find($id);
            return view('Admin.edit', [
                'info' => $info
            ]);
        }

        $username = $request->filled('username') ? $request->username : '';
        $password = $request->filled('password') ? $request->password : '';
        $role_id = $request->filled('role_id') ? $request->role_id : 0;
        $status = $request->filled('status') ? $request->status : 1;

        if($id){
            //update
            Admin::where('id', $id)->update([
                'username' => $username,
                'password' => $password,
                'role_id' => $role_id,
                'status' => $status
            ]);
        }else{
            //add
            if(Admin::where('username',$username)->first()){
                return json_encode([
                    'code' => 0,
                    'msg' => 'username can not duplicate[用户名不能重复]'
                ]);
            }

            $admin = new Admin;
            $admin->username = $username;
            $admin->password = $password;
            $admin->role_id = $role_id;
            $admin->status = $status;
            $admin->save();
        }

        return json_encode([
            'code' => 1,
            'msg' => 'success'
        ]);
    }

    public function del(Request $request){
        $id = $request->filled('id') ? $request->id : null;

        if(!$id)
            return json_encode([
                'code' => 0,
                'msg' => 'id can not be null'
            ]);

        $id_arr = explode(',', $id);
        foreach($id_arr as $v){
            //删除该管理员
            Admin::destroy($v);
        }

        return json_encode([
            'code' => 1,
            'msg' => 'success'
        ]);
    }

    public function login(Request $request){
        $username = $request->filled('username') ? $request->username : '';
        $password = $request->filled('password') ? $request->password : '';

        if(!Admin::where([['username','=',$username],['password','=',$password]])->first()){
            return json_encode([
                'code' => 0,
                'msg' => '账号或者密码错误'
            ]);
        }

        if(!Admin::where([['username','=',$username],['password','=',$password]])->first()['status']){
            return json_encode([
                'code' => 0,
                'msg' => '账号被禁用'
            ]);
        }

        //update last_ogin_ip
        Admin::where([
            ['username','=',$username],
            ['password','=',$password]
        ])->update([
            'last_login_ip' => $request->getClientIp()
        ]);

        //session
        $request->session()->put('username',$username);
        return json_encode([
            'code' => 1,
            'msg' => 'success'
        ]);
    }

    public function logout(Request $request){
        $request->session()->forget('username');
        return redirect('/login.html');
    }
}
